package com.qmx.qmxapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.qmx.qmxapp.R;
import com.qmx.qmxapp.adapter.JoinContactMultipleItemAdapter;
import com.qmx.qmxapp.base.BaseActivity;
import com.qmx.qmxapp.bean.ContactModel;
import com.qmx.qmxapp.bean.LzyResponse;
import com.qmx.qmxapp.callback.JsonCallback;
import com.qmx.qmxapp.constant.AppConstant;
import com.qmx.qmxapp.constant.ComParamContact;
import com.qmx.qmxapp.data.SectionMultipleItem;
import com.qmx.qmxapp.data.Video;
import com.qmx.qmxapp.token.TokenManager;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author ChengXiangtong
 * @create 2018/10/29
 * @Describe
 */
public class ContactGroupActivity extends BaseActivity {
    @BindView(R.id.title_content)
    TextView title_content;
    @BindView(R.id.tv_right_label)
    TextView tv_right_label;

    private List<String> list;
    private RecyclerView recyclerView;
    private Gson mGson = new Gson();
    private JSONObject jsonObject = new JSONObject();
    private List<SectionMultipleItem> modelList = null;
    private Intent intent = new Intent();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_group);
        ButterKnife.bind(this);
        title_content.setText("企业通讯录");
        list = new ArrayList<>();
        tv_right_label.setText("完成(" + list.size() + ")");

        recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        getDepList();

    }

    @OnClick(R.id.tv_right_label)
    public void onclick(View v) {
        switch (v.getId()) {
            case R.id.tv_right_label:
                onBackPressed();
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent(this, ConfereeActivity.class);
        intent.putStringArrayListExtra("uids", (ArrayList<String>) list);
        setResult(1, intent);
        finish();
        super.onBackPressed();
    }

    private void getData(final List<SectionMultipleItem> mData) {
        //         = getDepList();
        Log.i("mData", mData.size() + "");
        JoinContactMultipleItemAdapter sectionAdapter = new JoinContactMultipleItemAdapter(R.layout.def_section_head, mData);
        recyclerView.setAdapter(sectionAdapter);
        sectionAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                SectionMultipleItem item = (SectionMultipleItem) adapter.getData().get(position);
                switch (view.getId()) {
                    case R.id.card_view:
                        // 获取主体item相应数据给后期使用
                        if (item.getVideo() != null && hasLower(item.getVideo().getContent().toString())) {
                            intent.setClass(ContactGroupActivity.this, CustomerGroupActivity.class);
                            intent.putExtra("title", item.getVideo().getName());
                            intent.putExtra("name", item.getVideo().getImg());
                            intent.putExtra("content", item.getVideo().getContent().toString());
                            startActivity(intent);
                        } else {
                            CheckBox check = (CheckBox) adapter.getViewByPosition(recyclerView, position, R.id.cb_checked);
                            String uids = null;
                            try {
                                jsonObject = new JSONObject(item.getVideo().getContent().toString());
                                uids = jsonObject.opt("id").toString();
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            if (check.isChecked()) {
                                check.setChecked(false);
                                list.remove(uids);
                            } else {
                                check.setChecked(true);
                                list.add(uids);
                            }

                            Log.i("tv_right_label", item.getVideo().getContent().toString());
                            tv_right_label.setText("完成(" + list.size() + ")");

                            //                            intent.setClass(ContactGroupActivity.this, ContactDetailActivity.class);
                            //                            intent.putExtra("content", item.getVideo().getContent().toString());
                        }
                        Log.i("ContactsFragment", item.getVideo().getContent().toString());

                        break;
                    default:
                        Log.i("default---", item.getVideo().getContent().toString());
                        //                        Toast.makeText(getActivity(), "OnItemChildClickListener " + position, Toast.LENGTH_LONG).show();
                        break;
                }
            }
        });
    }

    public void getDepList() {
        modelList = new ArrayList<>();
        OkGo.<LzyResponse<List<ContactModel>>>post(AppConstant.Url + ComParamContact.contactList.PATH)
                .headers(ComParamContact.tokenCheck.TOKEN, TokenManager.getInstance().getAuthModel().getToken())
                .execute(new JsonCallback<LzyResponse<List<ContactModel>>>() {
                    @Override
                    public void onSuccess(Response<LzyResponse<List<ContactModel>>> response) {
                        String json = mGson.toJson(response.body().data.get(0));
                        ContactModel contactModel = mGson.fromJson(json, ContactModel.class);
                        if (contactModel.getContent() != null) {
                            Log.e("getDepartment", json);
                            if (contactModel.getContent().getDepartment() != null) {
                                for (ContactModel.Content.Department department : contactModel.getContent().getDepartment()) {
                                    Video video = new Video("department", department.getName(), mGson.toJson(department));
                                    Log.i("department", department.getName());
                                    modelList.add(new SectionMultipleItem(SectionMultipleItem.TEXT, video));
                                }
                            }
                            modelList.add(new SectionMultipleItem(true, "组织架构（启明星集团）", false));
                            if (contactModel.getContent().getDiscuss() != null) {
                                for (ContactModel.Content.Discuss discuss : contactModel.getContent().getDiscuss()) {
                                    Video video = new Video("discuss", discuss.getName(), mGson.toJson(discuss));
                                    modelList.add(new SectionMultipleItem(SectionMultipleItem.TEXT, video));
                                }
                            }
                            if (contactModel.getContent().getUsers() != null) {
                                for (ContactModel.Content.Users users : contactModel.getContent().getUsers()) {
                                    Video video = new Video(users.getAvatar(), users.getName(), mGson.toJson(users));
                                    modelList.add(new SectionMultipleItem(SectionMultipleItem.IMG_TEXT, video));
                                }
                            }
                        }
                        getData(modelList);
                        Log.i("ccmodelList", modelList.size() + "");
                    }

                    @Override
                    public void onError(Response<LzyResponse<List<ContactModel>>> response) {
                        Log.e("onError", response.toString());
                        //                            handleError(response);
                    }
                });
        Log.i("modelList", modelList.size() + "");
    }

}
